package com.atwisdom.star.core.bsl.aop;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.actionType.modelClass.logic.LogicActionType;
import com.atwisdom.star.common.actionType.modelClass.resource.ResourceActionType;
import com.atwisdom.star.common.vo.ActionData;
import com.atwisdom.star.common.vo.ResultInfo;
import com.atwisdom.star.core.bsl.business.Impl.DataChangeBslServiceImpl;
import com.atwisdom.star.core.bsl.business.utils.ConstantInfo;
import com.atwisdom.star.core.dao.pojo.modelClass.logic.LogicInstance;
import com.atwisdom.star.core.dao.pojo.modelClass.resource.ResourceInstance;
import com.atwisdom.star.core.dsl.modelClass.resource.Impl.ResourceInstanceDslServiceImpl;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Aspect
public class ResourceInstanceHandler {

    @Autowired
    private DataChangeBslServiceImpl dataChangeBslService;

    @Autowired
    private ResourceInstanceDslServiceImpl resourceInstanceDslService;

    @Pointcut("@annotation(com.atwisdom.star.core.bsl.aop.ResourceInstanceAnnotation)")//用于匹配带有注解的方法
    private void pointcut() {
    }

    @Before("pointcut()")
    public void myBefore(JoinPoint joinpoint) {
    }

    @After("pointcut()")
    public void myAfter(JoinPoint joinpoint) throws InterruptedException {
    }

    @Around("pointcut()")
    public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        ActionData actionData = (ActionData<JSONObject>) args[0];
        String action = actionData.getAction();
        ResourceActionType actionType = ResourceActionType.valueOf(action);
        String code = actionType.getCode();
        this.beforeDealSendData(code, actionData);
        Object proceed = joinPoint.proceed();
        this.afterDealSendData(code, proceed);
        return proceed;
    }

    private void afterDealSendData(String code, Object proceed) {
        if (code.equalsIgnoreCase(ResourceActionType.RESOURCE_INSTANCE_ADD.getCode())) {
            try {
                ResultInfo<JSONObject> resultInfo = (ResultInfo) proceed;
                JSONObject data = resultInfo.getData();
                dealSendDataForAdd(data);
            } catch (Exception ex) {
                System.out.println("新增数据上传ERP失败");
                ex.printStackTrace();
            }
        }
    }

    private void beforeDealSendData(String code, ActionData actionData) {
        if (code.equalsIgnoreCase(ResourceActionType.RESOURCE_INSTANCE_UPDATE_BY_ID.getCode())) {
            try {
                dealSendDataForUpdate(actionData);
            } catch (Exception ex) {
                System.out.println("更新数据上传ERP失败");
                ex.printStackTrace();
            }
        } else if (code.equalsIgnoreCase(ResourceActionType.RESOURCE_INSTANCE_DELETE_BY_IDS.getCode())) {
            try {
                dealSendDataForDelete(actionData);
            } catch (Exception ex) {
                System.out.println("删除数据上传ERP失败");
                ex.printStackTrace();
            }
        }
    }

    private void dealSendDataForAdd(JSONObject data) {
        dataChangeBslService.dealSendDataForInstance(data, "resource", ConstantInfo.OP_TYPE_ADD, false);
    }

    private void dealSendDataForUpdate(ActionData actionData) {
        List<JSONObject> data = actionData.getData();
        JSONObject jsonObject = data.get(0);
        dataChangeBslService.dealSendDataForInstance(jsonObject, "resource", ConstantInfo.OP_TYPE_UPDATE, false);
    }

    private void dealSendDataForDelete(ActionData actionData) {
        List<Long> idList = actionData.getIds();
        Long id = actionData.getId();
        if (CollectionUtil.isEmpty(idList)) {
            idList = new ArrayList<>();
            idList.add(id);
        } else {
            id = idList.get(0);
        }
        ResourceInstance resourceInstance = resourceInstanceDslService.queryInsById(id);
        if (resourceInstance == null) {
            return;
        }
        String className = resourceInstance.getClassName();
        boolean need = dataChangeBslService.needTrans(className, "resource", ConstantInfo.OP_TYPE_DELETE);
        if (need) {
            dataChangeBslService.apiSendDataBatchSave(idList, className, "resource", ConstantInfo.OP_TYPE_DELETE, false);
        }
    }
}
